﻿<?php
// hosts.php - 显示主机信息列表

// 数据库配置
$host = 'localhost';
$db   = 'host_registry';
$user = 'root';
$pass = 'root';
$charset = 'utf8mb4';

// 创建 PDO 连接
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];

try {
    $pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
    die("数据库连接失败：" . $e->getMessage());
}

// 查询主机信息
$stmt = $pdo->query("SELECT * FROM hosts ORDER BY last_seen DESC");
$hosts = $stmt->fetchAll();
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>主机在线列表</title>
    <style>
        body {
            font-family: "Microsoft YaHei", Arial, sans-serif;
            padding: 20px;
        }
        h2 {
            color: #333;
        }
        table {
            border-collapse: collapse;
            width: 100%;
            margin-top: 15px;
        }
        th, td {
            border: 1px solid #ccc;
            padding: 8px 12px;
            text-align: center;
        }
        th {
            background-color: #f2f2f2;
        }
        .online {
            background-color: #e1f8e1;
        }
    </style>
</head>
<body>
    <h2>登记主机列表（按最近上报时间排序）</h2>

    <table>
        <tr>
            <th>主机名</th>
            <th>操作系统</th>
            <th>局域网 IP</th>
            <th>远程 IP</th>
	    <th>MAC</th>
            <th>最后上报时间</th>
        </tr>
        <?php foreach ($hosts as $host): ?>
        <tr class="online">
            <td><?= htmlspecialchars($host['hostname']) ?></td>
            <td><?= htmlspecialchars($host['os']) ?></td>
            <td><?= htmlspecialchars($host['local_ip']) ?></td>
            <td><?= htmlspecialchars($host['remote_ip']) ?></td>
            <td><?= htmlspecialchars($host['mac_address']) ?></td>
            <td><?= htmlspecialchars($host['last_seen']) ?></td>
        </tr>
        <?php endforeach; ?>
    </table>
</body>
</html>
